package com.swas.service.impl;

import com.swas.dao.BaseDao;
import com.swas.dao.BaseDeviceDao;
import com.swas.dao.BaseStockDao;
import com.swas.dao.BaseTypeDao;
import com.swas.entity.*;
import com.swas.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lxysky
 */

@Transactional
@Service
public class BaseServiceImpl implements BaseService {

    @Autowired
    private BaseDao baseDao;

    @Autowired
    private BaseStockDao baseStockDao;

    @Autowired
    private BaseTypeDao baseTypeDao;

    @Autowired
    private BaseDeviceDao baseDeviceDao;


    @Override
    public List<Base> selectBaseAll() {
        BaseExample example = new BaseExample();
        example.createCriteria().andParentidEqualTo(0);
        List<Base> rootList = baseDao.selectByExample(example);
        List<Base> baseList = baseDao.selectByExample(null);
        tree(rootList, baseList);
        return rootList;
    }

    @Override
    public List<BaseStock> selectBaseStockAll() {
        return baseStockDao.selectByExample(null);
    }

    @Override
    public boolean deleteBaseStockById(Integer id) {
        return baseStockDao.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public boolean updateBaseTypeById(BaseType baseType) {
        return baseTypeDao.updateByPrimaryKeySelective(baseType) > 0;
    }

    @Override
    public BaseStock selectBaseStockById(Integer id) {
        return baseStockDao.selectByPrimaryKey(id);
    }

    @Override
    public boolean insertBaseStock(BaseStock baseStock) {
        baseStock.setUsecount(0);
        baseStock.setTotalmoney(baseStock.getPrice().multiply(new BigDecimal(baseStock.getBuycount())));
        return baseStockDao.insertSelective(baseStock) > 0;
    }

    @Override
    public boolean insertBase(BaseType baseType) {
        Base base = new Base();
        base.setName(baseType.getName());
        base.setParentid(baseType.getBaseid());
        base.setLink("");
        baseDao.insertSelective(base);
        return baseTypeDao.insertSelective(baseType) > 0;
    }

    @Override
    public BaseType selectBaseTypeByName(String name) {
        BaseTypeExample example = new BaseTypeExample();
        example.createCriteria().andNameEqualTo(name);
        List<BaseType> list = baseTypeDao.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<BaseDevice> selectBaseDevice(Integer id) {
        BaseDeviceExample example = new BaseDeviceExample();
        example.createCriteria().andStockchildidEqualTo(id);
        return baseDeviceDao.selectByExample(example);
    }

    @Override
    public boolean updateDeviceStatus(BaseDevice baseDevice) {
        return baseDeviceDao.updateByPrimaryKeySelective(baseDevice) > 0;
    }

    @Override
    public boolean addOneDevice(BaseDevice baseDevice) {
        baseDevice.setChildstatus("良好");
        return baseDeviceDao.insertSelective(baseDevice) > 0;
    }

    private void tree(List<Base> rootList, List<Base> baseList) {
        for (Base root : rootList){
            List<Base> children = new ArrayList<>();
            for (Base child : baseList){
                if(child.getParentid().equals(root.getId())){
                    children.add(child);
                }
            }
            if(children.size()>0){
                tree(children,baseList);
            }
            root.setChildren(children);
        }
    }
}
